/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.server.action.domain;



import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.common.utils.BifangConstants;
import han.bifang.common.utils.CommonUtils;
import han.bifang.common.utils.ProtocalUtils;
import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.CatalogBinding;
import han.bifang.gwt.client.common.entity.HttpBinding;
import han.bifang.gwt.client.common.entity.JmsBinding;
import han.bifang.gwt.client.common.entity.JmsBindingWrapper;
import han.bifang.gwt.client.common.entity.NormalJms;
import han.bifang.gwt.server.action.BifangGWTAction;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetCatalogBindingfromUrlAction implements BifangGWTAction {
	
	
	public ResponseObject execute(RequestObject req,HttpServletRequest request,HttpServletResponse response) throws BifangException {
		ResponseObject res = new ResponseObject();
		ArrayList<Object> p = (ArrayList<Object>)req.getRequest();
		String url = (String)p.get(0);
		String appServer = (String)p.get(1);
		String version = (String)p.get(2);
		
		try {
			// @todo check the url 
			if(false){
				
				throw new BifangException(BifangErrorCode.URL_ACCESS_ERROR,"url can't be access");
			}
			ArrayList ret = new ArrayList();
			CatalogBinding cb = new CatalogBinding();
			cb.setName(EntityConstants.CAT_BINDING_QUERY);
			HttpBinding hb = new HttpBinding();
			hb.setUrl(url + "/bifangWeb/query_v1");
			cb.setBinding(hb);
			cb.setType(EntityConstants.BINDING_HTTP);
			ret.add(cb);
			
			cb = new CatalogBinding();
			cb.setName(EntityConstants.CAT_BINDING_MONITOR);
			hb = new HttpBinding();
			hb.setUrl(url + "/bifangWeb/monitor_v1");
			cb.setBinding(hb);
			cb.setType(EntityConstants.BINDING_HTTP);
			ret.add(cb);
			
			url = CommonUtils.removeHTTPProtocal(url);
			cb = new CatalogBinding();
			cb.setName(EntityConstants.CAT_BINDING_SECURITY);
			hb = new HttpBinding();
			hb.setUrl("http://" + url + "/bifangWeb/security_v1");
			cb.setBinding(hb);
			cb.setType(EntityConstants.BINDING_HTTP);
			ret.add(cb);
			
			
			cb = new CatalogBinding();
			cb.setName(EntityConstants.CAT_BINDING_STATISTICS);
			JmsBinding jb = new JmsBinding();
			
			NormalJms info = new NormalJms();
			
			info.setServerUrl(ProtocalUtils.getJMSProtocal(appServer) + "://" + url);
			info.setDestinationJndi(BifangConstants.CAT_STATISTICS_JMS_QUEUE);
			info.setFactoryUrl(BifangConstants.CAT_STATISTICS_JMS_FAC);
			
			jb.setJmsType(EntityConstants.JMS_TYPE_QUEUE);
			jb.setServer(appServer);
			jb.setServerVersion(version);
			
			JmsBindingWrapper w = new JmsBindingWrapper();
			w.setInfo(info);
			w.setJmsBinding(jb);
			cb.setBinding(w);
			cb.setType(EntityConstants.BINDING_JMS);
			
			ret.add(cb);
			
			
					
			
			res.setCode("0");
			res.setResponse(ret);
		} catch (BifangException e) {
			throw e;	
		}catch (Exception e) {
			throw new BifangException(BifangErrorCode.UNKNOWN_ERROR,e.getMessage(),e);		
		}
		return res;
	}

}
